package com.yhq.basic.thread;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

/**
 * 查看一个main方法启动后，java有哪些线程
 * @author hqyin
 * @date 2023/11/29 11:19 AM
 */
public class ShowJavaThreads {
    private final static Integer obj = 1;
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized(obj)  {
                    System.out.println(Thread.currentThread().getName()+"获取锁成功");
                    try {
                        Thread.sleep(1000);
                        System.out.println(Thread.currentThread().getName()+"进入条件队列");
                        obj.wait();
                        System.out.println(Thread.currentThread().getName()+"被唤醒了");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
            }
        },"t1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized(obj)  {
                    System.out.println(Thread.currentThread().getName()+"获取锁成功");
                }
            }
        },"t2").start();


        synchronized(obj)  {
            System.out.println(Thread.currentThread().getName()+"主线程获取锁");
            obj.notifyAll();
        }

//        ThreadMXBean threadMXBean =  ManagementFactory.getThreadMXBean();
//        ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false,false);
//        for (ThreadInfo threadInfo : threadInfos) {
//            System.out.println("["+threadInfo.getThreadId()+"] "+threadInfo.getThreadName());
//        }
    }
}
